<!DOCTYPE html>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 -->
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova-2.2.0.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="js/InAppPurchaseManager.js"></script>
        <script type="text/javascript">
            app.initialize();
            
            function receiveNativeMsg(msg) {
                alert(msg);
                var form = document.form;
                form.nativeMsg.value = msg;
            }
            
            function callObjectiveC() {
                var form = document.form;
                var title = form.title.value;
                var name = form.name.value;
                var memo = form.memo.value;
                Cordova.exec(null, null, "JSReceiverPlugin", "receiveMessage", ['callJS',''+title+'', ''+name+'', ''+memo+'' ]);
            }
            
            function addInAppPurchase() {
                // Product ftk50
                console.log('addInAppPurchase');
                window.plugins = {};
                window.plugins.inAppPurchaseManager = InAppPurchaseManager.manager = new InAppPurchaseManager();
                window.plugins.applicationPreferences = new applicationPreferences();
                
                window.plugins.inAppPurchaseManager.requestProductData("com.pushinapptest.justwar.1000coin", function(productId, title, description, price) {
                                                                       console.log("productId: " + productId + " title: " + title + " description: " + description + " price: " + price);
                                                                       // verify product has already been purchases and unlock
                                                                       var productIds1 = ["com.pushinapptest.justwar.1000coin"];
                                                                       
                                                                       for (var i = 0; i < productIds1.length; i++) {
                                                                       var productId = productIds1[i];
                                                                       window.plugins.applicationPreferences.get(productId, function(result) {
                                                                                                                 if (result) {
                                                                                                                 unlockProduct(productId);
                                                                                                                 console.log("restart unlocked: " + productId);
                                                                                                                 }
                                                                                                                 });
                                                                       }
                                                                       },
                                                                       
                                                                       function(id) {
                                                                       console.log("Invalid product id: " + id);
                                                                       
                                                                       });
                
                window.plugins.inAppPurchaseManager.requestProductData("com.pushinapptest.justwar.2000coin", function(productId, title, description, price) {
                                                                       console.log("productId: " + productId + " title: " + title + " description: " + description + " price: " + price);
                                                                       // verify product has already been purchases and unlock
                                                                       var productIds1 = ["com.pushinapptest.justwar.2000coin"];
                                                                       
                                                                       for (var i = 0; i < productIds1.length; i++) {
                                                                       var productId = productIds1[i];
                                                                       window.plugins.applicationPreferences.get(productId, function(result) {
                                                                                                                 if (result) {
                                                                                                                 unlockProduct(productId);
                                                                                                                 console.log("restart unlocked: " + productId);
                                                                                                                 }
                                                                                                                 });
                                                                       }
                                                                       },
                                                                       
                                                                       function(id) {
                                                                       console.log("Invalid product id: " + id);
                                                                       
                                                                       });
                
                // unlock product
                function unlockProduct(productId) {
                    var   a = "com.pushinapptest.justwar.1000coin",
                    b = "com.pushinapptest.justwar.2000coin";
                    
                    if(productId == a) {
                        $('.ftk50 .button-background div:nth-child(1n)').hide();
                        $('.ftk50 .button-background').addClass('highlight-color');
                        $('.ftk50 .button-background div:nth-child(2n)').fadeIn();
                    }
                    
                    if(productId == b) {
                        $('.ftk49 .button-background div:nth-child(1n)').hide();
                        $('.ftk49 .button-background').addClass('highlight-color');
                        $('.ftk49 .button-background div:nth-child(2n)').fadeIn();
                    }
                    
                    console.log('unlocked: ' + productId);
                }
                
                // unlock product
                window.plugins.inAppPurchaseManager.onPurchased = function(transactionIdentifier, productId, transactionReceipt) {
                    console.log('purchased: ' + productId);
                    
                    window.plugins.applicationPreferences.set(productId, true, function() {
                                                              unlockProduct(productId);
                                                              });
                }
                
                
                // restore products
                window.plugins.inAppPurchaseManager.onRestored = function(transactionIdentifier, productId, originalTransactionReceipt) {
                    window.plugins.applicationPreferences.set(productId, true, function() {
                                                              unlockProduct(productId);
                                                              //  $('#popupPadded h2').removeClass('loader');
                                                              $('.restore-purchases-btn').hide();
                                                              $('.restore-purchases-btn-complete').fadeIn();
                                                              
                                                              $('.purchase-message').hide();
                                                              $('.purchase-message-complete').show();
                                                              
                                                              });
                    console.log('restored: ' + productId);
                }
                
                // onFailed
                window.plugins.inAppPurchaseManager.onFailed = function(errno, errtext) {
                    console.log('failed: ' + errtext);
                }
            }
            
            function startPurchase() {
                console.log("start Purchase");
                window.plugins.inAppPurchaseManager.makePurchase('com.pushinapptest.justwar.1000coin', 1);
                console.log("end Purchase");
            }
            </script>
        <form name="form">
            Title : <input type="text" name="title"/><br>
            Name : <input type="text" name="name"/><br>
            Memo : <input type="text" name="memo"/><br>
            <input type="button" value="Objective-C Call" onClick="callObjectiveC()"/>
            <hr>
            <textarea name="nativeMsg" style="width:200px; height:50px;"></textarea>
        </form>
        
        <input type="button" value="inApp" onClick="addInAppPurchase()"/><br>
        <input type="button" value="Buy Now" data-ajax="false" id="ftk49" onClick="startPurchase()"/>
        
        <script type="text/javascript" src="js/applicationPreferences.js"></script>
    </body>
</html>